package com.wicket.okrbff.web.contorller;

import cn.hutool.core.bean.BeanUtil;
import com.wicket.okrbff.biz.service.TargetContComService;
import com.wicket.okrbff.biz.service.dto.req.*;
import com.wicket.okrbff.biz.service.dto.res.*;
import com.wicket.okrbff.common.ro.OkrResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.wicket.okrapp.integration.FwAppTargetContComClient;
import com.wicket.okrapp.integration.dto.ImplementBrushTempTargetContTreeOrderNumRespDto;
import com.wicket.okrapp.integration.dto.ImplementBrushTempTargetContTreeOrderNumReqDto;
import com.wicket.okrbff.common.exception.Assert;
import com.wicket.okrbff.common.util.CommonFunctionHelper;
import com.wicket.okrapp.integration.dto.QueryTargetCycleAncestorDataOriginListRespDto;
import com.wicket.okrapp.integration.dto.QueryTargetCycleAncestorDataOriginListReqDto;
import com.wicket.okrbff.biz.service.dto.common.BffEvaObjTargetCycleDto;
import java.util.stream.Collectors;
import com.wicket.okrapp.integration.dto.QueryTargetContCrumbsListRespDto;
import com.wicket.okrapp.integration.dto.QueryTargetContCrumbsListReqDto;
import com.wicket.okrbff.common.dto.TreeNode;
import com.wicket.okrapp.integration.FwAppEvaThemeClient;
import com.wicket.okrcomponent.integration.FwCompInterfaceModeClient;
import com.wicket.okrcomponent.integration.FwCompTemplateEngineClient;
import com.wicket.okrcomponent.integration.FwCompConfSchemeClient;
import com.wicket.okrapp.integration.dto.QueryPlanningDataExportDetailListComRespDto;
import com.wicket.okrapp.integration.dto.QueryPlanningDataExportDetailListComReqDto;
import com.wicket.okrapp.integration.dto.QueryEvaTempDetailComRespDto;
import com.wicket.okrapp.integration.dto.QueryEvaTempDetailComReqDto;
import com.wicket.okrcomponent.integration.dto.ImplementTimeTurnToStringComRespDto;
import com.wicket.okrcomponent.integration.dto.ImplementTimeTurnToStringComReqDto;
import com.wicket.okrcomponent.integration.dto.GenerateSemanticInstanceStoryComRespDto;
import com.wicket.okrcomponent.integration.dto.GenerateSemanticInstanceStoryComReqDto;
import com.wicket.okrcomponent.integration.dto.QueryConfItemMatchAnswerListComRespDto;
import com.wicket.okrapp.integration.dto.QueryConfItemMatchAnswerListComReqDto;
import com.wicket.okrcomponent.integration.dto.ImplementExportDatatoExcelComRespDto;
import com.wicket.okrcomponent.integration.dto.ImplementExportDatatoExcelComReqDto;
import com.wicket.okrcomponent.integration.dto.ExcelHeaderFieldDataDto;
import com.wicket.okrcomponent.integration.dto.ExportDataDto;
import cn.hutool.core.collection.CollectionUtil;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：
 * @date ：
 *
 *
 * @version 1.0
 */
@Api(value = "TargetContCom", tags = { "TargetContCom" })
@RestController
// @RequestMapping("/oms/targetContCom/")
public class TargetContComController {

  @Autowired
  TargetContComService targetContComService;
/**
   * B3执行刷全模板目标内容树排序(管理)
   * gen by moon at 5/17/2024, 2:40:26 AM
   */
    @PostMapping("/oms/mgt/targetContCom/implementBrushTempTargetContTreeOrderNum")
    @ApiOperation(value = "B3执行刷全模板目标内容树排序(管理)", response = OkrResult.class)
    public OkrResult<BffImplementBrushTempTargetContTreeOrderNumRespDto> implementBrushTempTargetContTreeOrderNum(@RequestBody(required=false) @Validated BffImplementBrushTempTargetContTreeOrderNumReqDto reqDTO){
        return OkrResult.success(targetContComService.implementBrushTempTargetContTreeOrderNum(reqDTO));
    }
/**
   * B3查询目标周期祖先数据源列表
   * gen by moon at 5/17/2024, 3:09:58 AM
   */
    @PostMapping("/oms/targetContCom/queryTargetCycleAncestorDataOriginList")
    @ApiOperation(value = "B3查询目标周期祖先数据源列表", response = OkrResult.class)
    public OkrResult<BffQueryTargetCycleAncestorDataOriginListRespDto> queryTargetCycleAncestorDataOriginList(@RequestBody(required=false) @Validated BffQueryTargetCycleAncestorDataOriginListReqDto reqDTO){
        return OkrResult.success(targetContComService.queryTargetCycleAncestorDataOriginList(reqDTO));
    }
/**
   * B3查询目标内容面包屑列表
   * gen by moon at 8/9/2024, 4:05:19 PM
   */
    @PostMapping("/oms/targetContCom/queryTargetContCrumbsList")
    @ApiOperation(value = "B3查询目标内容面包屑列表", response = OkrResult.class)
    public OkrResult<BffQueryTargetContCrumbsListRespDto> queryTargetContCrumbsList(@RequestBody(required=false) @Validated BffQueryTargetContCrumbsListReqDto reqDTO){
        return OkrResult.success(targetContComService.queryTargetContCrumbsList(reqDTO));
    }
/**
   * B3查询规划数据导出内容明细列表
   * gen by moon at 1/1/2025, 5:57:37 PM
   */
    @PostMapping("/oms/targetContCom/queryPlanningDataExportDetailList")
    @ApiOperation(value = "B3查询规划数据导出内容明细列表", response = OkrResult.class)
    public void queryPlanningDataExportDetailList(@RequestBody(required=false) @Validated BffQueryPlanningDataExportDetailListReqDto reqDTO, HttpServletResponse response){
        targetContComService.queryPlanningDataExportDetailList(reqDTO,(excelData)->{
            if(excelData.containsKey("exportFileFormat")){
                String exportFileFormat = (String)excelData.get("exportFileFormat");
                String exportExcelFileName = (String)excelData.get("exportExcelFileName");
                List<ExcelHeaderFieldDataDto> headerDataList = (List<ExcelHeaderFieldDataDto>)excelData.get("excelHeaderFieldDataList");
                List<ExportDataDto> dataList = (List<ExportDataDto>)excelData.get("exportDataList");

                String fileFullNameTmp = exportExcelFileName + "." +exportFileFormat;
                String fileFullName = null;
                try {
                    fileFullName = URLEncoder.encode(fileFullNameTmp, "UTF-8").replaceAll("\\+", "%20");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileFullName);
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                Workbook workbook = null;
                try {
                    workbook = new XSSFWorkbook();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                // 创建一个工作表sheet
                Sheet sheet = workbook.createSheet("Sheet1");

                // 创建表头行
                Row headerRow = sheet.createRow(0);
                int column = 0;
                Cell numCell = headerRow.createCell(column++);
                numCell.setCellValue("编号");
                for (ExcelHeaderFieldDataDto field : headerDataList) {
                    Cell cell = headerRow.createCell(column++);
                    cell.setCellValue(field.getExcelHeaderCnFields());
                }

                // 填充数据
                long rowCount = 1l;
                for (int i = 0; i < dataList.size(); i++) {
                    Row row = sheet.createRow(i + 1);
                    ExportDataDto rowData = dataList.get(i);
                    for (int j = 0; j < 11; j++) {
                        Cell cell = row.createCell(j);
                        if(j==0){
                            cell.setCellValue(rowCount);
                        }else if(j==1 && rowData.getExportDataFields1()!=null){
                            cell.setCellValue(rowData.getExportDataFields1());
                        }else if(j==2 && rowData.getExportDataFields2()!=null){
                            cell.setCellValue(rowData.getExportDataFields2());
                        }else if(j==3 && rowData.getExportDataFields3()!=null){
                            cell.setCellValue(rowData.getExportDataFields3());
                        }else if(j==4 && rowData.getExportDataFields4()!=null){
                            cell.setCellValue(rowData.getExportDataFields4());
                        }else if(j==5 && rowData.getExportDataFields5()!=null){
                            cell.setCellValue(rowData.getExportDataFields5());
                        }else if(j==6 && rowData.getExportDataFields6()!=null){
                            cell.setCellValue(rowData.getExportDataFields6());
                        }else if(j==7 && rowData.getExportDataFields7()!=null){
                            cell.setCellValue(rowData.getExportDataFields7());
                        }else if(j==8 && rowData.getExportDataFields8()!=null){
                            cell.setCellValue(rowData.getExportDataFields8());
                        }else if(j==9 && rowData.getExportDataFields9()!=null){
                            cell.setCellValue(rowData.getExportDataFields9());
                        }else if(j==10 && rowData.getExportDataFields10()!=null){
                            cell.setCellValue(rowData.getExportDataFields10());
                        }
                    }
                    rowCount++;
                }

                // 自动调整列宽
                for (int i = 0; i < headerDataList.size(); i++) {
                    sheet.autoSizeColumn(i);
                }

                // 将工作簿写入文件
                try {
                    ServletOutputStream outputStream = response.getOutputStream();
                    workbook.write(outputStream);
                    workbook.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        workbook.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        });
        return ;
    }

    /**
     * B3查询规划数据导出内容明细列表
     * gen by moon at 1/1/2025, 5:57:37 PM
     */
    @GetMapping("/oms/targetContCom/queryPlanningDataExportDetailListGet")
    @ApiOperation(value = "B3查询规划数据导出内容明细列表", response = OkrResult.class)
    public void queryPlanningDataExportDetailListGet(@RequestParam("levelNumber") Long levelNumber,
                                                     @RequestParam("isLastObjective") String isLastObjective,
                                                     @RequestParam("cycleId") String cycleId,
                                                     @RequestParam("parentSubMidCycleType") String parentSubMidCycleType,
                                                     @RequestParam("evaluationTemplateId") String evaluationTemplateId,
                                                     HttpServletResponse response){
        BffQueryPlanningDataExportDetailListReqDto reqDTO = new BffQueryPlanningDataExportDetailListReqDto();
        reqDTO.setLevelNumber(levelNumber);
        reqDTO.setIsLastObjective(isLastObjective);
        reqDTO.setCycleId(cycleId);
        reqDTO.setParentSubMidCycleType(parentSubMidCycleType);
        reqDTO.setEvaluationTemplateId(evaluationTemplateId);
        targetContComService.queryPlanningDataExportDetailList(reqDTO,(excelData)->{
            if(excelData.containsKey("exportFileFormat")){
                String exportFileFormat = (String)excelData.get("exportFileFormat");
                String exportExcelFileName = (String)excelData.get("exportExcelFileName");
                List<ExcelHeaderFieldDataDto> headerDataList = (List<ExcelHeaderFieldDataDto>)excelData.get("excelHeaderFieldDataList");
                List<ExportDataDto> dataList = (List<ExportDataDto>)excelData.get("exportDataList");

                String fileFullNameTmp = exportExcelFileName + "." +exportFileFormat;
                String fileFullName = null;
                try {
                    fileFullName = URLEncoder.encode(fileFullNameTmp, "UTF-8").replaceAll("\\+", "%20");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileFullName);
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                Workbook workbook = null;
                try {
                    workbook = new XSSFWorkbook();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                // 创建一个工作表sheet
                Sheet sheet = workbook.createSheet("Sheet1");

                // 创建表头行
                Row headerRow = sheet.createRow(0);
                int column = 0;
                Cell numCell = headerRow.createCell(column++);
                numCell.setCellValue("编号");
                for (ExcelHeaderFieldDataDto field : headerDataList) {
                    Cell cell = headerRow.createCell(column++);
                    cell.setCellValue(field.getExcelHeaderCnFields());
                }

                // 填充数据
                long rowCount = 1l;
                for (int i = 0; i < dataList.size(); i++) {
                    Row row = sheet.createRow(i + 1);
                    ExportDataDto rowData = dataList.get(i);
                    for (int j = 0; j < 11; j++) {
                        Cell cell = row.createCell(j);
                        if(j==0){
                            cell.setCellValue(rowCount);
                        }else if(j==1 && rowData.getExportDataFields1()!=null){
                            cell.setCellValue(rowData.getExportDataFields1());
                        }else if(j==2 && rowData.getExportDataFields2()!=null){
                            cell.setCellValue(rowData.getExportDataFields2());
                        }else if(j==3 && rowData.getExportDataFields3()!=null){
                            cell.setCellValue(rowData.getExportDataFields3());
                        }else if(j==4 && rowData.getExportDataFields4()!=null){
                            cell.setCellValue(rowData.getExportDataFields4());
                        }else if(j==5 && rowData.getExportDataFields5()!=null){
                            cell.setCellValue(rowData.getExportDataFields5());
                        }else if(j==6 && rowData.getExportDataFields6()!=null){
                            cell.setCellValue(rowData.getExportDataFields6());
                        }else if(j==7 && rowData.getExportDataFields7()!=null){
                            cell.setCellValue(rowData.getExportDataFields7());
                        }else if(j==8 && rowData.getExportDataFields8()!=null){
                            cell.setCellValue(rowData.getExportDataFields8());
                        }else if(j==9 && rowData.getExportDataFields9()!=null){
                            cell.setCellValue(rowData.getExportDataFields9());
                        }else if(j==10 && rowData.getExportDataFields10()!=null){
                            cell.setCellValue(rowData.getExportDataFields10());
                        }
                    }
                    rowCount++;
                }

                // 自动调整列宽
                for (int i = 0; i < headerDataList.size(); i++) {
                    sheet.autoSizeColumn(i);
                }

                // 将工作簿写入文件
                try {
                    ServletOutputStream outputStream = response.getOutputStream();
                    workbook.write(outputStream);
                    workbook.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        workbook.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        });
        return ;
    }
}
